<template>
  <div class="app-container bg-gray">
    <el-card class="box-1">
      <div class="header">
        <div>
          <el-select v-model="searchTerms.status" placeholder="问卷状态" class="select" clearable @change="getData">
            <el-option :value="1" label="正在进行" />
            <el-option :value="0" label="有结束" />
            <el-option :value="-1" label="已过期" />
          </el-select>
          <el-date-picker
            v-model="searchTerms.date"
            :editable="false"
            class="picker"
            type="daterange"
            range-separator="-"
            start-placeholder="开始日期"
            end-placeholder="结束日期"
            value-format="yyyy-MM-dd"
            @change="getData"/>
          <el-input v-model="searchTerms.searchValue" placeholder="请输入问卷名称" class="search-input" @change="getData" />
          <el-button type="success" class="do-btn" icon="el-icon-search" @click="getData">搜索</el-button>
        </div>
        <div>
          <el-button type="primary" icon="el-icon-plus" @click="toCreateQuestionnaire">创建问卷</el-button>
        </div>
      </div>
      <el-table v-loading="tableLoading" :data="tableData" :max-height="tableMaxHeight" border highlight-current-row @sort-change="handleSortChange">
        <el-table-column prop="id" label="ID" width="100" header-align="center" align="center" sortable />
        <el-table-column prop="title" label="问卷" min-width="160"/>
        <el-table-column prop="total" label="回收量" width="140" header-align="center" align="center">
          <template slot-scope="scope">
            {{ scope.row['member_name']?scope.row['member_name']:'--' }}
          </template>
        </el-table-column>
        <el-table-column prop="created_at" label="创建时间" width="200" header-align="center" align="center">
          <template slot-scope="scope">
            {{ parseTime(scope.row['created_at'], '{y}-{m}-{d}') }}
          </template>
        </el-table-column>
        <el-table-column label="状态" width="200" header-align="center" align="center">
          <template slot-scope="scope">
            <el-tag v-if="scope.row['status']===0" type="success">未开始</el-tag>
            <el-tag v-if="scope.row['status']===1" type="info">进行中</el-tag>
            <el-tag v-if="scope.row['status']===2" type="info">已结束</el-tag>
          </template>
        </el-table-column>
        <el-table-column label="操作" width="300" header-align="center" align="center" fixed="right">
          <template slot-scope="scope">
            <el-button v-if="scope.row['status']===0" type="primary" size="small" @click="toInfo(scope.row)">投放问卷</el-button>
            <el-button v-if="scope.row['status']===0" type="danger" size="small" @click="toModify(scope.row)">结束问卷</el-button>
            <el-button v-if="scope.row['status']===0" type="success" class="bg-green" size="small" @click="toAnswerSheet(scope.row)">答卷列表</el-button>
            <el-button v-if="scope.row['status']===2" type="info" size="small" @click="toModify(scope.row)">导出答卷</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :current-page.sync="currentPage"
        :page-size="pageSize"
        :page-sizes="pageSizes"
        :total="total"
        class="pagination"
        layout="total, sizes, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        @size-change="handleSizeChange"/>
    </el-card>
  </div>
</template>

<script>
import { handleSortChange, handleSizeChange, handleCurrentChange, parseTime } from '@/utils/table'

export default {
  data() {
    return {
      downloadLoading: false,
      searchTerms: {
        state: '',
        date: [],
        searchValue: ''
      },

      // 表格相关数据
      tableMaxHeight: document.documentElement.clientHeight - 380,
      tableLoading: false,
      currentPage: 1,
      pageSize: this.$store.getters.userInfo.pageSize,
      pageSizes: [10, 20, 50, 100, 1000],
      total: 0,
      tableData: []
    }
  },
  created() {
    this.getData()
  },
  methods: {
    handleCurrentChange, handleSizeChange, handleSortChange, parseTime,
    getData(order) {
      this.tableLoading = true
      order || (order = {})
      const url = 'ask/getDataList'
      const data = {
        page_no: this.currentPage,
        page_size: this.pageSize,
        search: this.searchTerms.searchValue,
        status: this.searchTerms.status,
        type: this.searchTerms.type
      }
      this.searchTerms.date && this.searchTerms.date.length !== 0 && (data.start_time = this.searchTerms.date[0] + ' 00:00:00', data.end_time = this.searchTerms.date[1] + ' 23:59:59')
      order.sortName && (data.sortName = order.sortName)
      order.sortOrder && (data.sortOrder = order.sortOrder)
      this.$store.dispatch('GetConnect', { url, data }).then(res => {
        this.tableData = res.data.rows
        this.total = res.data.total
        this.tableLoading = false
      }).catch(e => {
        this.tableLoading = false
        this.$message.error(e.msg + ',请刷新或联系管理员')
      })
    },

    toAnswerSheet(row) {
      this.$router.push({
        path: '/questionnaire/answerSheet',
        query: { id: row.id }
      })
    },

    toCreateQuestionnaire() {
      this.$router.push({
        path: '/questionnaire/create'
      })
    }
  }
}
</script>

<style lang="scss" type="text/scss" scoped>
@import '../../../styles/index.scss';
.box-1 {
  .model {
    .el-card {
      color: #fff;
      background-color: $panGreen;
      span {
        font-size: 14px;
      }
      div {
        font-size: 24px;
        margin-top: 10px;
      }
    }
  }
  .header {
    @include flex-row(center, space-between);
    flex-wrap: wrap;
    padding-bottom: 15px;
    div {
      @include flex-row(center, flex-start);
      flex-wrap: wrap;
      .select {
        padding-right: 15px;
        width: 150px;
      }
      .picker {
        margin-right: 15px;
        width: 300px;
      }
      .do-btn {
        @extend .bg-green;
        margin-left: 6px;
      }
      .search-input {
        width: 180px;
      }
    }
  }
}
</style>

